let arr1 = [1, 2, 3, 4, 4, 1]
let arr2 = [3, 4, 5, 4]

const arr = [...new Set([...arr1, ...arr2])] // [1,2,3,4,5]
const newArr = []
arr.forEach((item) => {
  const newArr1 = arr1.filter((item1) => item1 === item)
  const newArr2 = arr2.filter((item2) => item2 === item)
  if (newArr1.length && newArr2.length) {
    if (newArr1.length <= newArr2.length) {
      newArr.push(...newArr1)
    } else {
      newArr.push(...newArr2)
    }
  }
})
console.log(newArr)
